package com.douqu.game.core.entity.ext;

import com.douqu.game.core.entity.DropObject;
import com.douqu.game.core.entity.LotteryObject;
import com.douqu.game.core.entity.RewardsObject;
import com.douqu.game.core.entity.WorldInfo;
import com.douqu.game.core.entity.common.CommonData;
import com.douqu.game.core.entity.sprite.Card;
import com.douqu.game.core.entity.sprite.Player;
import com.douqu.game.core.factory.ConstantFactory;
import com.douqu.game.core.factory.DataFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author: Gavin.
 * Description:抽奖
 * Date: 2017/8/17 0017 上午 9:05
 * Huan Yu Copyright (c) 2017 All Rights Reserved.
 */
public class LotteryInfo extends ExtInfo {


    public LotteryInfo(Player player, WorldInfo worldInfo) {
        super(player, worldInfo);
    }



    private Map<Integer,Integer> currentNum; //当前已抽奖次数


    private Map<Integer,Integer> data = null; //当前抽奖级别 key:类型 value:对应的当前奖池级别

    private Map<Integer,Long> lotteryTime = null; //免费抽奖时间

    /***
     * 获取剩余次数
     * @return
     */
    public int getSurplusCount(){
        return 0;
    }


    /***
     * 存储抽奖的次数
     * @param key
     * @param value
     */
    public void addCurrentNum(int key ,int value){
        if(currentNum == null || currentNum.size() == ConstantFactory.ZERO || currentNum.isEmpty())
            currentNum = new ConcurrentHashMap<Integer,Integer>();
        currentNum.put(key,value);
    }

    /***
     * 根据类型获取当前抽奖次数
     * @param key
     * @return
     */
    public int getCurrentNumByKey(int key){
        return currentNum == null ? 0 : currentNum.get(key);
    }


    /***
     * 根据类型修改抽奖次数
     * @param key
     * @param value
     */
    public void updateCurrentNumByKey(int key,int value){
        if(!currentNum.isEmpty()){
            currentNum.remove(key);
            currentNum.put(key,value);
        }
    }


    /***
     * 金币抽
     */
    public void getFreeLottery(int type,int buyType){

        List<LotteryObject> list = DataFactory.getInstance().getDataList(DataFactory.LOTTERY_KEY);
//
//        List<LotteryObject> temp = new ArrayList<>();
//        for(LotteryObject lotteryObject : list){
//            if(type == lotteryObject.getType()){
//                temp.add(lotteryObject);
//            }
//        }


        //当前奖池
        int currentLottery = data.get(type);
        //已经抽了的次数
        int count = currentNum.get(currentLottery);

        //根据奖池获取当前对象
        LotteryObject lotteryObject = DataFactory.getInstance().getGameObject(DataFactory.LOTTERY_KEY, currentLottery);

        if(lotteryObject == null)
            return;

        //当前的奖励类型
        int rewardType = lotteryObject.getRewardType();

        //免费次数
        int freeCount = lotteryObject.getFree();

        //获取资源数组
        CommonData commonData = getCommonDatas(lotteryObject);
        if(commonData == null)
            return;

        //获取奖励数组
        RewardsObject[] rewards = lotteryObject.getRewards();
        if(rewards == null)
            return;

        //获取背包资源
        BagInfo bagInfo = (BagInfo) player.getExtInfo(BagInfo.class);
        Map<Integer,Integer> assetData = bagInfo.getAssetData();
        if(assetData == null){
            //背包资源为空
            return;
        }
        //当前时间
        long currentTime = System.currentTimeMillis();

        //单次
        if(buyType == 1) {

            if((currentTime - lotteryTime.get(lotteryObject.id)) / 1000 <= lotteryObject.cd){
                //cd中暂时不能抽奖
                return;
            }

            //设置当次的抽奖时间
            lotteryTime.put(lotteryObject.id,System.currentTimeMillis());
            //已抽奖次数等于9次到达下一级次数
            if(count >= lotteryObject.getNextLevelCount()) {

                //已抽奖的次数设置为0
                currentNum.put(currentLottery, 0);
                //升级到下一奖池
                lotteryObject = DataFactory.getInstance().getGameObject(DataFactory.LOTTERY_KEY, lotteryObject.getNextLevelPond());

                //抽奖业务逻辑
                resourceDegradation(assetData,commonData,rewards,freeCount,currentLottery,rewardType,bagInfo);

                //当前抽奖级别 key:类型 value:对应的当前奖池级别
                data.put(type, lotteryObject.id);
                //修改抽奖次数
                currentNum.put(currentLottery, 1);
            } else {
                //执行抽奖
                resourceDegradation(assetData,commonData,rewards,freeCount,currentLottery,rewardType,bagInfo);
                currentNum.put(currentLottery, count+1);
            }
        } else {
            if(count >= lotteryObject.getNextLevelCount()) {

            }
        }

    }


    /***
     * 消耗资源，获取奖励
     * @param assetData
     * @param commonData
     * @param rewards
     */
    private void resourceDegradation( Map<Integer,Integer> assetData,CommonData commonData,RewardsObject[] rewards,int freeCount,int currentLottery,int rewardType,BagInfo bagInfo){
        //所需消耗资源
        Integer currentAsset =  assetData.get(commonData.id);
        int rsAsset = currentAsset == null ? ConstantFactory.ZERO : currentAsset.intValue();

        //如果免费次数大于当前抽奖的次数
        if(freeCount > currentNum.get(currentLottery)){
            //判断短背包资源是否足够
            if(rsAsset < commonData.value){
                //背包资源不足
                return;
            }else{
                //减去背包的资源
                assetData.put(commonData.id,rsAsset- commonData.value);
            }
        }

//        //奖励道具
//        List<RewardsObject> rewardsList = new ArrayList<RewardsObject>();
        if(rewards == null ){
            //获取配置文件有误
            return;
        }else{
            int c = 0;
            for(int i = 0 ;i < rewards.length; i++){
                c += rewards[i].odds;
            }
            int r = new Random(c).nextInt();
            int t = 0;
            for(int j = 0 ;j < rewards.length; j++){
                t += rewards[j].odds;
                if(r <= t){
                    if(rewardType == 1){
                        bagInfo.addAsset(rewards[j].getId(),rewards[j].getCount());
                        break;
                    }
                    if(rewardType == 2){
                        bagInfo.addGoods(null, rewards[j].getId(),rewards[j].getCount());
                        break;
                    }
                    if(rewardType == 3){
                        //获取背包中已有的卡片
                        List<Card> cardList = bagInfo.getCardList();
                        for (Card cards: cardList){
                            bagInfo.addCard(null, rewards[j].getId(), rewards[j].getCount());
                        }


                    }
                }
            }
        }


    }

    public void setCurrentNum(Map<Integer, Integer> currentNum) {
        this.currentNum = currentNum;
    }

    public Map<Integer, Integer> getCurrentNum() {
        return currentNum;
    }


    /***
     * 获取资源对象
     * @param lotteryObject
     * @return
     */
    public CommonData getCommonDatas(LotteryObject lotteryObject){

        if(lotteryObject.getCommonDatas().length > 0)
            return lotteryObject.getCommonDatas()[0];
        else
            return null;
    }

}
